import { createRouter, createWebHistory } from 'vue-router'
import userLogin from '@/components/login/login.vue'
import userIndex from '@/components/main/index.vue'
import admin from '@/components/system/admin.vue'
import information from '@/components/system/information.vue'
import orders from '@/components/system/orders.vue'

const router = createRouter(
  {
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
      {
        path: '/',
        redirect: '/login'
      },
      {
        path: '/index',
        name: 'userIndex',
        component: userIndex,
        redirect: '/information',
        children: [
          {
            path: '/admin',
            component: admin,
          },
          {
            path: '/information',
            component: information
          },
          {
            path: '/orders',
            component: orders
          }
        ]
      },
      {
        path: '/login',
        name: 'userLogin',
        component: userLogin
      },
    ],
  },
);

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('Authorization');

    if (token === null || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

export default router
